
global filename jmp_results_forecast_temp_bins


use "stata data/regsave/FE_new_all_tempbins_event", clear
append using "stata data/regsave/FE_new_all_tempbins_coastal_event"
append using "stata data/regsave/FE_new_all_tempbins_inland_event"

gen rd = 0


replace rd = 1 if rd == .

duplicates drop
order rd cz


capture order type column 

//here I drop the pdp one in the marginal since the marginal effect comes from the pdp_non_event variable




drop tstat	ci_lower	ci_upper	N	r2

gen stars = ""
replace stars = "*" if pval<.1
replace stars = "**" if pval<.05
replace stars = "***" if pval<.01

*replace stars =""

gen label = ""
replace label = "Peak pricing $\times$ day-ahead forecast 96-97 degrees (F)" if var == "pdp_by_temp_bin_1"
replace label = "Peak pricing $\times$ day-ahead forecast 98-99 degrees (F)" if var == "pdp_by_temp_bin_2"
replace label = "Peak pricing $\times$ day-ahead forecast 100-101 degrees (F)" if var == "pdp_by_temp_bin_3"
replace label = "Peak pricing $\times$ day-ahead forecast 102-104 degrees (F)" if var == "pdp_by_temp_bin_4"



replace label = "Temperature" if var == "temp_f"
replace label = "Temperature squared" if var == "temp_f_2"

order label

capture file close table1		
file open table1 using "latex/$filename.tex", write text replace


file write table1 "\label{$filename}" _n
file write table1 "\vspace{-2mm}" _n
*file write table1 "\footnotesize" _n
*file write table1 "\begin{tabular}{lcccccc}" _n //this is where you change justification. Options are l c r

file write table1 "\begin{tabular}{@{\extracolsep{0pt}}lcc@{\hskip .5cm}cc@{\hskip .5cm}cc}" _n //this is where you change justification. Options are l c r


*file write table1 "\begin{tabular}{@{\extracolsep{4pt}}lcc@{\hskip .5cm}" _n //this is where you change justification. Options are l c r
*extracolsep from http://tex.stackexchange.com/questions/101000/how-to-shorten-cline-so-that-multiple-groups-in-a-table-become-visible

file write table1 "\hline\hline \\ [-.9em]" _n 

*file write table1 "\multicolumn{1}{c}{$\begin{matrix}\text{Baseline}\\ \text{Territory} \end{matrix}$}& 	\multicolumn{1}{c}{$\begin{matrix}\text{Average}\\ \text{Temperature} \end{matrix}$} &	\multicolumn{1}{c}{$\begin{matrix}\text{Firm}\\ \text{Count} \end{matrix}$}	&	\\" _n
*file write table1 " Energy use classification && Median & & &Thirds	\\" _n 
*file write table1 "  \cline{2-4} \cline{5-7} \\" _n 
*file write table1 "[0.2em]" _n

*file write table1 "  & \multicolumn{1}{c}{ Inland } \\" _n 


*file write table1 "  && Structural winners & & &Structural losers\\" _n 
*file write table1 "  \cline{2-3}  \\" _n 
*file write table1 "[-0.8em]" _n
file write table1 "  & (1) & (2) & (3)   \\" _n
file write table1 "[0.2em]" _n
file write table1 "  & All PG\&E  & Coastal & Inland \\" _n
file write table1 "\hline \\" _n
file write table1 "[-0.7em]" _n



capture gen row = _n

gen avg_kwh_high_EI_label = ""

tostring rd, replace

gen column_id = cz + "_rd_" + rd
order column_id

order column_id

local col1 "all_rd_0"
local col2 "coastal_rd_0"
local col3 "inland_rd_0"



foreach v in "pdp_by_temp_bin_1" "pdp_by_temp_bin_2" "pdp_by_temp_bin_3" "pdp_by_temp_bin_4" "temp_f" "temp_f_2" { //this first loop looks through pdp_high_EIs producing and services providing

foreach col in "`col1'" "`col2'" "`col3'" { //"`col2'" "`col3'" "`col4'" "`col5'" "`col6'" { //this loop loops through event day and non-event day

local panel "event"  // "non_event" "marginal"{ //this loop loops through event day and non-event day

sum row if var == "`v'" & col == "`col'" & type == "`panel'" //this grabs the correct row. So goods event is row number ___ - I then grab that NUMBER observation out of variable
local r = r(mean) //just a way to indicate what row you're on from the sum term above


local name_`col'_`panel' = label[`r'] 
local c1_`col'_`panel' = "\$"+string(coef[`r'],"%9.5f")+ "$" + stars[`r'] 
local c2_`col'_`panel' = "(" +"\$"+ string(stderr[`r'],"%9.5f")+"$"  + ")"

local avg_kwh_high_EI_label = avg_kwh_high_EI_label[`r'] 
*local s1_`col'_`panel' = string(avg_kwh_high_EI[`r'],"%-9.2f")
}

file write table1 "`name_`col1'_`panel'' & `c1_`col1'_`panel'' & `c1_`col2'_`panel'' & `c1_`col3'_`panel''    \\" _n //the COEF line. Include title
file write table1 " 					 & `c2_`col1'_`panel'' & `c2_`col2'_`panel'' & `c2_`col3'_`panel''  \\" _n //the standard error line. The & at the front is b/c no titles on this line
file write table1 "[1.5ex] " _n //this adds vertical space between the se/coef groupings

}

///
///Marginal
///


gen obs_label = "Observations"
gen firms_label = "Establishments"
gen event_day_kwh_label = "Event day kWh usage"

capture drop money_pdp_saved
gen money_pdp_saved = coef * 4 * .6 * -1
gen money_pdp_saved_label = "Dollar savings"

gen temp_f_label = "Average temperature"


foreach v in "pdp_by_temp_bin_1"  { //this first loop looks through pdp_high_EIs producing and services providing

foreach col in "`col1'" "`col2'" "`col3'" { //"`col2'" "`col3'" "`col4'" "`col5'" "`col6'" { //this loop loops through event day and non-event day

sum row if var == "`v'" & col == "`col'" & type == "event" //this grabs the correct row. So goods event is row number ___ - I then grab that NUMBER observation out of variable
local r = r(mean) //just a way to indicate what row you're on from the sum term above


local obs_label = obs_label[`r'] 
local s1_`col' = "\$" + string(obs[`r'],"%-9.0fc") +"$"

local firms_label = firms_label[`r'] 
local s2_`col' = "\$" + string(firms[`r'],"%-9.0fc") +"$"

local event_day_kwh_label = event_day_kwh_label[`r'] 
local s3_`col' = "\$" + string(avg_kwh[`r'],"%-9.2fc") +"$"


local temp_f_label = temp_f_label[`r'] 
local s4_`col' = "\$" + string(temp_f[`r'],"%-9.0fc") +"$"


}
*file write table1 "`firms_label' & `s1_5' & `s1_8'  \\" _n 
file write table1 "`obs_label' 		 & `s1_`col1'' & `s1_`col2'' & `s1_`col3'' \\" _n 
file write table1 "`firms_label' 		 & `s2_`col1'' & `s2_`col2'' & `s2_`col3'' \\" _n 
file write table1 "`event_day_kwh_label' & `s3_`col1'' & `s3_`col2'' & `s3_`col3''  \\" _n 
file write table1 "`temp_f_label' 		 & `s4_`col1'' & `s4_`col2'' & `s4_`col3''  \\" _n 


}
*




file write table1 "\hline" _n
file write table1 "\end{tabular}" _n

file close table1		

